package cn.sitemaker.app.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import cn.sitemaker.app.R;
import cn.sitemaker.app.adapter.RecomandAdapter;
import cn.sitemaker.app.base.BaseFragment;
import cn.sitemaker.app.bean.PositionEntity;
import cn.sitemaker.app.task.InputTipTask;
import cn.sitemaker.app.task.PoiSearchTask;
import cn.sitemaker.app.task.RouteTask;

/**
 * 目的地界面
 * 
 * @author kymjs(kymjs123@gmail.com)
 */
public class DistinationFragment extends BaseFragment implements View.OnClickListener,TextWatcher,AdapterView.OnItemClickListener {

    private ListView mRecommendList;

    private ImageView mBack_Image;

    private TextView mSearchText;

    private EditText mDestinaionText;

    private RecomandAdapter mRecomandAdapter;

    private RouteTask mRouteTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup view = (ViewGroup) inflater.inflate(
                R.layout.fragment_destination, container, false);
        mRecommendList=(ListView) view.findViewById(R.id.recommend_list);

        mSearchText=(TextView) view.findViewById(R.id.destination_search);
        mSearchText.setOnClickListener(this);

        mDestinaionText=(EditText) view.findViewById(R.id.destination_edittext);
        mDestinaionText.addTextChangedListener(this);
        mRecomandAdapter=new RecomandAdapter(getContext());
        mRecommendList.setAdapter(mRecomandAdapter);
        mRecommendList.setOnItemClickListener(this);

        mRouteTask=RouteTask.getInstance(getContext());

        return view;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        InputTipTask.getInstance(getContext(), mRecomandAdapter).searchTips(s.toString(),
                RouteTask.getInstance(getContext()).getStartPoint().city);


    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){

            case R.id.destination_search:
                PoiSearchTask poiSearchTask=new PoiSearchTask(getContext(), mRecomandAdapter);
                poiSearchTask.search(mDestinaionText.getText().toString(),RouteTask.getInstance(getContext()).getStartPoint().city);
                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long id) {

        PositionEntity entity = (PositionEntity) mRecomandAdapter.getItem(position);
        if (entity.latitue == 0 && entity.longitude == 0) {
            PoiSearchTask poiSearchTask=new PoiSearchTask(getContext(), mRecomandAdapter);
            poiSearchTask.search(entity.address,RouteTask.getInstance(getContext()).getStartPoint().city);

        } else {
            mRouteTask.setEndPoint(entity);
            mRouteTask.search();

        }
    }
}
